from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.http import *
from django.shortcuts import *
from django.views import *
from home.models import *
from users.models import *
import json
# Create your views here.

class IndexView(View):
    def get(self,request):
        # 获取所有分类信息数据1
        articleCategories = ArticleCategory.objects.all()

        #接受用户点击的分类id
        cat_id = request.GET.get('cat_id',1)

        #根据分类id进行分类的查询
        try:
            select_articleCategory = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
            select_articleCategory = ArticleCategory.objects.get(id=1)

        article = Article.objects.filter(category=select_articleCategory)


        # 获取页面请求传递的页码，页容量
        page_index = request.GET.get('page_index',1)
        page_size = request.GET.get('page_size',2)

        # 根据被选中的文章分类显示他下的所有

        #使用django的分页器
        pages = Paginator(article,page_size)

        try:
            list = pages.page(page_index)
        except PageNotAnInteger:
            list = pages.page(1)
        except EmptyPage:
            list = pages.page(pages.num_pages)

        if request.COOKIES.get("login_name") is not None:
            username = json.loads(request.COOKIES.get("login_name"))
        else:
            username = ""

        total_views = Article.objects.order_by('-total_views')[0:2]


        context = {
            "articles": list,
            "cat_id": cat_id,
            "total_views":total_views,
        }

        return render(request, 'index.html', context=context)

class DetailView(View):
    def get(self,request):
        art_id = request.GET.get('artid')
        try:
            article = Article.objects.get(id=art_id)
        except Article.DoesNotExist:
            return render(request,'404.html')

        #浏览量。思路：每次访问该页面是对他进行加1
        article.total_views += 1
        article.save()

        #最新文章
        news_art = Article.objects.order_by('-create_time')[0:2]

        #重新查询文章信息。按照浏览量降序排序
        hot_tags = Article.objects.order_by('-total_views').distinct()[:9]

        #
        relations = Article.objects.filter(tags=article.tags)[:3]

        #获取所有评论信息
        from home.models import Comment
        comm = Comment.objects.filter(article=article).order_by('-created_time')
        context = {
            "article":article,
            "comms":comm,
            "newArt":news_art,
            'relations': relations,
            "hotTags":hot_tags,
        }
        return render(request, 'details.html',context=context)

    def post(self,request):
        user = request.user

        if user and user.is_authenticated:
            art_id = request.POST.get('art_id')
            content = request.POST.get('content')

            try:
                art = Article.objects.get(id=art_id)
            except Article.DoesNotExist:
                return HttpResponseBadRequest('该文章不存在')

            # 保存评论数据
            from home.models import Comment
            Comment.objects.create(content=content,article=art,user=user)

            #修改文章的评论数量
            art.comments_count += 1
            art.save()


            #刷新当前页面
            req_url = request.path + '?artid=' + art_id
            return redirect(req_url)
        else:
            #未登录用户直接转到登录页面
            return redirect(reverse('users:login'))
